#include <stdio.h> 
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdint.h>

#include "sm3.h"
#include "common_func.h"
#include "core_time.h"

//BSP板级支持包所需全局变量
unsigned long UART_BASE = 0xbf000000;					//UART16550的虚地址
unsigned long CONFREG_TIMER_BASE = 0xbf20f100;			//CONFREG计数器的虚地址
unsigned long CONFREG_CLOCKS_PER_SEC = 50000000L;		//CONFREG时钟频率
unsigned long CORE_CLOCKS_PER_SEC = 33000000L;			//处理器核时钟频率

int main(int argc, char** argv)
{
	printf("\nHello Loongarch32r SM3!\n");
	
	//delay_ms(1000);

	uint32_t ret = 0;
	
	uint32_t data_input[16] = {
		0xf93719cd, 0xf93719cd, 0xf93719cd, 0xf93719cd, 
		0xf93719cd, 0xf93719cd, 0xf93719cd, 0xf93719cd,
		0xf93719cd, 0xf93719cd, 0xf93719cd, 0xf93719cd, 
		0xf93719cd, 0xf93719cd, 0xf93719cd, 0xf93719cd};
	uint32_t result[8] = {
		0xfcb048ea, 0x84e4b7f9, 0x8a81f837, 0x63851a69,
		0x36b377d8, 0x3c884ce9, 0xabffda76, 0x260f520a};
	uint32_t data_hash[8] = {0};
	
	SM3_Handle_t s_handle;
	memset(&s_handle, 0x0, sizeof(SM3_Handle_t));
	
	printf("data_input = ");
	for(int i = 0; i < 16; i++)
	{
		printf("0x%08x ", data_input[i]);
	}
	printf("\n");

	printf("Start init SM3.\n");
	sm3_init(&s_handle);
	printf("Updataing SM3.\n");
	for(int i = 0; i < 16; i++)
	{
		sm3_update(&s_handle, data_input[i]);
	}
	printf("Final clear SM3.\n");
	sm3_final(&s_handle, data_hash);
	printf("Expected results = ");
	for(int i = 0; i < 8; i++)
	{
		printf("0x%08x ", result[i]);
	}
	printf("\n");
	printf("data_hash = ");
	for(int i = 0; i < 8; i++)
	{
		printf("0x%08x ", data_hash[i]);
	}
	printf("\n");
	
	if (memcmp(data_hash, result, 8) == 0) 
	{
        printf("SM3 success.\n");
    } else {
        printf("SM3 Faild.\n");
    }
	
	printf("SM3 test finished!\n");

	return 0;
}